package com.woniuxy.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.yoga.pojo.Coach;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.yoga.pojo.Orders;
import com.woniuxy.yoga.pojo.Users;
import com.woniuxy.yoga.service.OrdersService;
import com.woniuxy.yoga.service.UsersService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author axq
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/yoga/users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/hello")
    public R get(){
        Users users = usersService.getAll();
        if(users!=null){
            return R.ok().data("users",users);
        }else{
            return R.error();
        }
    }
    @GetMapping("/users")
    public R get2(){
//        List<Users> users = usersService.getAll2();
        List<Users> list = usersService.list();
        if(list!=null){
            return R.ok().data("users",list);
        }else{
            return R.error();
        }
    }


    @ApiOperation("修改个人信息")
    @PutMapping("/update")
    @CrossOrigin
    public R updateMessage(@RequestBody Users form2){
        System.out.println(form2);
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        if(form2!=null){
            wrapper.eq("accountid",form2.getAccountid());
            Users user = usersService.getOne(wrapper);
            form2.setUserid(user.getUserid());
            boolean b = usersService.updateById(form2);
            if (b){
                return R.ok().message("修改成功");
            }else {
                return R.error().message("修改失败");
            }
        }
        return R.error().message("修改失败");

    }


    @CrossOrigin
    @ApiOperation("查询(登录本人)(身份学员)个人信息")
    @GetMapping(value = "my/{accountid}")
    public R getMyself(@PathVariable Integer accountid){
        QueryWrapper query=new QueryWrapper();
        query.eq("accountid",accountid);
        Users one = usersService.getOne(query);
        return R.ok().data("my",one).message("查询成功");
    }

    @ApiOperation(value = "得到单个用户")
    @GetMapping("/getOneUser/{uid}")
    @CrossOrigin
    public R getOneUser(@PathVariable("uid") Integer uid){
        Users user = usersService.getById(uid);
        if(user!=null){
            return R.ok().data("user",user);
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "更改用于余额")
    @GetMapping("/updatePrice/{price}/{uid}/{ordered}")
    @CrossOrigin
    public R updatePrice(@PathVariable("price") BigDecimal price, @PathVariable("uid") Integer uid,@PathVariable("ordered") Integer ordered){
        //得到当前用户
        Users user = usersService.getById(uid);
        Users user1=new Users();
        //余额进行减
        BigDecimal p=user.getMoney();
//        if(p.compareTo(BigDecimal.ZERO)==-1){
//            return R.error();
//        }
        user1.setMoney(p.subtract(price));
        user1.setUserid(uid);
        QueryWrapper<Users> q=new QueryWrapper<Users>(user1);
        //更新账户余额
        boolean update = usersService.updateById(user1);
        //修改订单状态
        Orders o=new Orders();
        o.setOrdered(ordered);
        o.setStatus(3);
        o.setCompletiontime(new Date());
        boolean b = ordersService.updateById(o);
        if(update&&b){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

